package com.healthyeating.manage.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.healthyeating.manage.domain.vo.AssocNutritionVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.AssocNutrition;
import com.healthyeating.manage.service.IAssocNutritionService;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 饮食和营养关联Controller
 * 
 * @author 段奇乐
 * @date 2024-12-30
 */
@RestController
@RequestMapping("/manage/nutritionBinding")
public class AssocNutritionController extends BaseController
{
    @Autowired
    private IAssocNutritionService assocNutritionService;

    /**
     * 查询饮食和营养关联列表
     */
    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:list')")
    @GetMapping("/list")
    public TableDataInfo list(AssocNutrition assocNutrition)
    {
        startPage();
        Map<String, Object> map = assocNutritionService.selectAssocNutritionList(assocNutrition);
        List<AssocNutrition> oldList = new ArrayList<>();
        List<AssocNutritionVo> newList = new ArrayList<>();
        if (map.get("oldList") != null) {
            oldList = (List<AssocNutrition>) map.get("oldList");
        }

        if (map.get("newList") != null) {
            newList = (List<AssocNutritionVo>) map.get("newList");
        }
//        List<AssocNutritionVo> list = assocNutritionService.selectAssocNutritionList(assocNutrition);
        return getDataTable(oldList, newList);
    }

//    /**
//     * 导出饮食和营养关联列表
//     */
//    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:export')")
//    @Log(title = "饮食和营养关联", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, AssocNutrition assocNutrition)
//    {
//        List<AssocNutritionVo> list = assocNutritionService.selectAssocNutritionList(assocNutrition);
//        ExcelUtil<AssocNutritionVo> util = new ExcelUtil<>(AssocNutritionVo.class);
//        util.exportExcel(response, list, "饮食和营养关联数据");
//    }

    /**
     * 获取饮食和营养关联详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(assocNutritionService.selectAssocNutritionById(id));
    }

    /**
     * 新增饮食和营养关联
     */
    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:add')")
    @Log(title = "饮食和营养关联", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<AssocNutrition> assocNutritionList)
    {
        return toAjax(assocNutritionService.insertAssocNutrition(assocNutritionList));
    }

    /**
     * 修改饮食和营养关联
     */
    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:edit')")
    @Log(title = "饮食和营养关联", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssocNutrition assocNutrition)
    {
        return toAjax(assocNutritionService.updateAssocNutrition(assocNutrition));
    }

    /**
     * 删除饮食和营养关联
     */
    @PreAuthorize("@ss.hasPermi('manage:nutritionBinding:remove')")
    @Log(title = "饮食和营养关联", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(assocNutritionService.deleteAssocNutritionByIds(ids));
    }
}
